package pl.kosmowska.ewelina.client.views.impl;

import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.InvestmentDataProxy;
import pl.kosmowska.ewelina.client.views.InvestmentView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanDialog;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent.Selected;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEventHandler;
import pl.kosmowska.ewelina.client.views.widgets.grids.InvestmentDataGrid;
import pl.kosmowska.ewelina.client.views.widgets.popup.PopupMessage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class InvestmentViewImpl extends Composite implements InvestmentView{

	private static InvestmentViewImplUiBinder uiBinder = GWT
			.create(InvestmentViewImplUiBinder.class);

	@UiField DockLayoutPanel investmentLayout;
	@UiField ResizeLayoutPanel investmentPanel;
	@UiField Button addInvestment;
	@UiField Button editInvestment;
	@UiField Button deleteInvestment;
	private BooleanDialog wconfirm;


	interface InvestmentViewImplUiBinder extends UiBinder<Widget, InvestmentViewImpl> {
	}

	private Presenter presenter;
	private boolean alreadyInit;


	private InvestmentDataGrid<InvestmentDataProxy> investmentgrid;

	public InvestmentViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void reInitPagingDataGrid( ){
		investmentgrid = new InvestmentDataGrid<InvestmentDataProxy>();
		investmentgrid.setHeight("600px");		
		investmentPanel.clear();
		investmentPanel.add(investmentgrid);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	@Override
	public void reInitView(List<InvestmentDataProxy> investmentDataList) {
		if (investmentDataList == null || investmentDataList.size() == 0) {
			showMessage(ViewMessages.NO_DATA_TO_SHOW);
		}else{
			reInitPagingDataGrid();
			investmentgrid.setDataList(investmentDataList);
		}

	}


	@UiHandler("deleteInvestment")
	void onDeleteInvestmentClick(ClickEvent event) {
		if(investmentgrid.getSelectedItem()!=null){
			initDeleteDialog();
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_DELETE);
		}
	}
	
	@UiHandler("editInvestment")
	void onEditInvestmentClick(ClickEvent event) {
		if(investmentgrid.getSelectedItem()!=null){
			presenter.goToInvestmentEditPlace(investmentgrid.getSelectedItem());
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_EDIT);
		}

	}
	@UiHandler("addInvestment")
	void onAddInvestmentClick(ClickEvent event) {
		presenter.goToInvestmentAddPlace();
	}

	private void initDeleteDialog() {
		if (wconfirm == null) {
			wconfirm = new BooleanDialog(ViewMessages.CONFIRM_DELETE);
			wconfirm.addSelectionHandler(new BooleanEventHandler() {
				public void onBooleanEvent(BooleanEvent event) {
					if (event.getBooleanEvent() == Selected.TRUE) {
						presenter.deleteInvestmentData(investmentgrid.getSelectedItem());
					} else if (event.getBooleanEvent() == Selected.FALSE) {
						// do nothing
					}
				}
			});
		}
		wconfirm.center();
	}

	@Override
	public void showMessage(String errorMessage) {
//		MessageDialogBox messageDialogBox = new MessageDialogBox(errorMessage);
//		messageDialogBox.center();
//		messageDialogBox.show();
        final PopupMessage alertMessage = new PopupMessage(errorMessage);
        alertMessage.startProcessing();
        Timer t = new Timer() {
            public void run() {
                alertMessage.stopProcessing();
            }
        };
        t.schedule(1000);

	}

	@Override
	public void updateAfterInvestmentDataDelete(InvestmentDataProxy selectedItem) {
		investmentgrid.removeFromDataList(selectedItem);
		
	}

	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}

	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}  
	
	
}


